<?php

namespace Config;

/**
 * 数据库配置
 *
 * @version 1.0
 * @author 张云江
 */
class DbConfig
{
    const configkey = "__DBKEY__";
    /**
     * 设置数据库配置
     * @param mixed $configname 配置名
     * @param mixed $host 主机地址
     * @param mixed $port 端口
     * @param mixed $username 用户名
     * @param mixed $pwd 用户密码
     * @param mixed $databasename 数据库名称
     * @param mixed $charset 
     */
    public static function setConfig($configname, $host, $port, $username, $pwd, $databasename, $charset = "utf8mb4")
    {
        $configname = (string)$configname;
        $data = null;
        if (array_key_exists(self::configkey, $GLOBALS))
            $data = $GLOBALS[self::configkey];
        if (!is_array($data))
            $data = array();
        $data[$configname] = self::setDbConfig($host, $port, $username, $pwd, $databasename, $charset);
        $GLOBALS[self::configkey] = $data;
    }
    private static function setDbConfig($host, $port, $username, $pwd, $databasename, $charset)
    {
        return array(
            'host' => $host,
            'username' => $username,
            'pwd' => $pwd,
            'port' => $port,
            'databasename' => $databasename,
            'charset' => $charset
        );
    }
    /**
     * 获取数据库配置
     * @param mixed $configname 配置名
     * @return mixed
     */
    public static function getConfig($configname)
    {
        $configname = (string)$configname;
        $data = $GLOBALS[self::configkey];
        if (array_key_exists($configname, $data))
            return $data[$configname];
        return array();
    }
}
